# -*- coding: utf-8 -*-
import json
import logging
from datetime import timedelta

from common.campaign.db.daily_recharge_return import set_daily_recharge
from common.campaign.model.daily_recharge_return import DAILY_RECHARGE_TYPE
from common.lottery.model import TrackIndex
from common.preset.db.banner import get_banner_by_type
from common.preset.model.preset import BANNER_TYPE
from common.stats import MG_BIGBANG_COLL
from common.utils.tz import now_ts, today_str, local_now, get_utc_date

_LOGGER = logging.getLogger('bigbang')


def get_campaign_status(user_id):
    banner = get_banner_by_type(BANNER_TYPE.DAILY_RECHARGE)

    if not banner:
        return {
            'status': 'not_available',
        }
    if now_ts() > banner.campaign_end:
        return {
            'status': 'ended',
        }
    if now_ts() < banner.campaign_start:
        return {
            'status': 'not_started',
            'left_seconds': banner.campaign_start - now_ts()
        }
    if banner.start_ts <= now_ts() <= banner.campaign_end:
        return {
            'status': 'ongoing',
            'recharge_amount': add_new_recharge(user_id),
        }


def add_new_recharge(user_id):
    user_data = MG_BIGBANG_COLL.daily_stats.find_one({'_id': '%s-%s' % (user_id, today_str())})
    if not user_data:
        return 0
    rech_amount = float(user_data.get('recharge', {'total': 0}).get('total', 0))
    set_daily_recharge(user_id, rech_amount, today_str())
    return rech_amount
